package com.indeep.user.mapper.po;

import com.baomidou.mybatisplus.annotation.*;
import com.indeep.user.dto.VxAuthDTO;
import com.indeep.user.dto.VxRefreshTokenDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.Objects;

@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("app_vx_auth")
public class AppVxAuthPO {
    private static final Logger log = LoggerFactory.getLogger(AppVxAuthPO.class);
    @TableId(type = IdType.AUTO)
    private Long id;
    private String openid;
    private String accessToken;
    private String refreshToken;
    private String unionid;
    
    /**
     * 创建时间-创建时自动填充
     */
    @TableField(fill = FieldFill.INSERT)
    private Date createTime;
    
    /**
     * 更新时间-创建与修改时自动填充
     */
    @TableField(fill = FieldFill.INSERT_UPDATE)
    private Date updateTime;
    
    public AppVxAuthPO(VxAuthDTO dto) {
        this.openid = dto.getOpenid();
        this.accessToken = dto.getAccess_token();
        this.refreshToken = dto.getRefresh_token();
        this.unionid = dto.getUnionid();
    }
    
    public void update(VxRefreshTokenDTO vxRefreshTokenBO) {
        if (!Objects.equals(this.openid, vxRefreshTokenBO.getOpenid())) {
            log.error("微信授权信息数据库实体更新失败！");
            return;
        }
        this.openid = vxRefreshTokenBO.getOpenid();
        this.accessToken = vxRefreshTokenBO.getAccess_token();
        this.refreshToken = vxRefreshTokenBO.getRefresh_token();
    }
}
